package com.lw.leetcode.node;

import com.lw.leetcode.node.TreeNode;

/**
 * @Author liw
 * @Date 2021/4/16 16:38
 * @Version 1.0
 */
public class LowestCommonAncestor {

    public static void main(String[] args) {

    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        while (true) {
            if (root == null) {
                break;
            }
            int a = root.val;
            int b = p.val;
            int c = q.val;
            if (b > c) {
                b = c;
                c = p.val;
            }
            if (a >= b && a <= c) {
                break;
            }
            if (a < b) {
                root = root.right;
            } else {
                root = root.left;
            }
        }
        return root;
    }

}
